package com.pd.web.controller.admin;

import com.alibaba.fastjson.JSON;
import com.pd.business.ResourceService;
import com.pd.constant.SysContant;
import com.pd.dao.mapper.ResourceDao;
import com.pd.dao.model.ResourceDo;
import com.pd.domain.ResponseVo;
import com.pd.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by dong peng on 2018/1/25.
 */
@Controller
@RequestMapping("/admin/resource")
public class ManageResourceCtl extends ManageBaseController {

    @Qualifier("resourceService")
    @Autowired
    private ResourceService resourceService;

    @RequestMapping("/query")
    public @ResponseBody String query() {
        ResponseVo responseVo = new ResponseVo();
        List<ResourceDo> result = resourceService.query();
        responseVo.setTip(SysContant.SUCCESS);
        responseVo.addRetData("rows",result);
        return JSON.toJSONString(responseVo);
    }

    @RequestMapping("/add")
    public @ResponseBody ResponseVo add(@RequestBody ResourceDo resourceDo) {
        resourceService.add(resourceDo);
        ResponseVo responseVo = new ResponseVo();
        responseVo.setTip(SysContant.SUCCESS);
        return responseVo;
    }

    @RequestMapping("/modify")
    public @ResponseBody ResponseVo modify(@RequestBody ResourceDo resourceDo) {
        resourceService.modify(resourceDo);
        ResponseVo responseVo = new ResponseVo();
        responseVo.setTip(SysContant.SUCCESS);
        return responseVo;
    }

    @RequestMapping("/delete")
    public @ResponseBody ResponseVo delete(@RequestBody ResourceDo resourceDo) {
        if(resourceDo == null || resourceDo.getId() == null) {
            throw  new BusinessException("没有传入正确id");
        }
        resourceService.delete(resourceDo.getId());
        ResponseVo responseVo = new ResponseVo();
        responseVo.setTip(SysContant.SUCCESS);
        return responseVo;
    }
}
